package scau.gsmg.misp.utils;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

import scau.gsmg.misp.vo.User;

/**
 * @Title: ShiroUtil.java
 * @Group: GaoShenMenGui
 * @Date: 2015-5-8
 * @description:获取当前shiro的信息
 */
public class ShiroUtil {
    /**
     * @description:从凭证组中获取当前用户，仅获取第一个realm中的第一个对象
     * 
     */
    public static User getUserByPrincipals(Subject subject) {
        String realmName = subject.getPrincipals().getRealmNames().iterator().next();
        User user = (User) subject.getPrincipals().fromRealm(realmName).iterator().next();
        return user;
    }

    /**
     *  @description:使用principals和realm获取放到principalCollection中的User对象
     *  
     */
    public static User getUserByPrincipalsRealmName(PrincipalCollection principals, String realmName) {
        User user = (User) principals.fromRealm(realmName).iterator().next();
        return user;
    }
    
    /**
	 *  @description:获取当前用户session
	 *  
	 */
	public static Session getSession(){
		Session session =SecurityUtils.getSubject().getSession();
		return session;
	}
	
	
	
	/**
	 *  @description:获取当前用户对象
	 * 
	 */
	public static User getCurrentUser(){
		Session session =SecurityUtils.getSubject().getSession();
		if(null!=session){
			return (User) session.getAttribute("user");
		}else{
			return null;
		}
		
	}
}
